package juniors.servlet.ci;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import juniors.modelo.CentroEstudio;
import juniors.modelo.Idea;
import juniors.negocio.GestionCentroEstudio;
import juniors.dao.CentroEstudioDAO;
import juniors.excepcion.DAOExcepcion;


/**
 * Servlet implementation class BuscarCentroEstudio
 */
@WebServlet("/BuscarCentroEstudio")
public class CeBuscarServlet extends javax.servlet.http.HttpServlet implements
			javax.servlet.Servlet { 
	private final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CeBuscarServlet() {
        super();
        System.out.println("servlet");
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("get");
		String nombre = request.getParameter("nombre");
		System.out.println(nombre);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		System.out.println("post");
		String nombre = request.getParameter("nombre");
		System.out.println(nombre);
		
		CentroEstudio centroestudio = new CentroEstudio();
		CentroEstudioDAO dao = new CentroEstudioDAO();
		GestionCentroEstudio negocio = new GestionCentroEstudio();
		
		try {
			Collection<CentroEstudio> lista = negocio.buscarporNombre(nombre);
			System.out.println("Iniciar busqueda");
			for(CentroEstudio x : lista)	{
				System.out.println(x.getNombre()+" "+x.getDireccion());
				
			}
			System.out.println("fin busqueda");	
			request.setAttribute("ceListado", lista);
			RequestDispatcher rd = request.getRequestDispatcher("ceBuscar.jsp");
			rd.forward(request, response);

		} catch (DAOExcepcion e) {
			System.out.println(e.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
	}

}
